package stage2.module2.Q1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        Connection connection = DBUtils.getConnection();
        PreparedStatement p1 = null, p2 = null;
        ResultSet r1 = null, r2 = null;

        if (connection == null) {
            return;
        }

        try {
            p1 = connection.prepareStatement("select * from employee where name = ?;");
            p2 = connection.prepareStatement("select * from employee where gender = ?;");

            // 筛选 名字是 bb 的所有记录
            p1.setString(1, "bb");
            // 筛选 性别是 男 的所有记录
            p2.setString(1, "男");

            r1 = p1.executeQuery();
            r2 = p2.executeQuery();

            // 查看使用名字的搜索结果
            System.out.println("开始打印使用名字的筛选结果：");
            while (r1.next()) {
                int i = r1.getInt("id");
                String name = r1.getString("name");
                String gender = r1.getString("gender");
                double salary = r1.getDouble("salary");
                double bonus = r1.getDouble("bonus");
                Date joinDate = r1.getDate("join_date");
                System.out.println(i + " " + name + " " + gender + " " + salary + " " + bonus + " " + joinDate);
            }

            // 查看使用性别的搜索结果
            System.out.println("开始打印使用性别的筛选结果：");
            while (r2.next()) {
                int i = r2.getInt("id");
                String name = r2.getString("name");
                String gender = r2.getString("gender");
                double salary = r2.getDouble("salary");
                double bonus = r2.getDouble("bonus");
                Date joinDate = r2.getDate("join_date");
                System.out.println(i + " " + name + " " + gender + " " + salary + " " + bonus + " " + joinDate);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (r2 != null) {
                    r2.close();
                }
                if (r1 != null) {
                    r1.close();
                }
                if (p2 != null) {
                    p2.close();
                }
                if (p1 != null) {
                    p1.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
